#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include <SDL/SDL.h>
#include "SDL/SDL_Image.h"
#include "PNG_MainMenu.h"
void update_button_state(bouton T[], int n){
    int x, y, i;
    if(SDL_GetMouseState(&x,&y) &SDL_BUTTON(1)){
        for (i=0; i<n; i++){
            if( x >= T[i].dest.x && x <= (T[i].dest.x+T[i].dest.w) && y >= T[i].dest.y && y <= (T[i].dest.y+T[i].dest.h))
                T[i].state=2;
            else
                T[i].state=0;
        }
    }else{
        for (i=0; i<n; i++){
            if( x >= T[i].dest.x && x <= (T[i].dest.x+T[i].dest.w) && y >= T[i].dest.y && y <= (T[i].dest.y+T[i].dest.h))
                T[i].state=1;
            else
                T[i].state=0;
        }
    }
    //for(i=0;i<n;i++)
        //T[i].state=0;
}

void display_menu (bouton T[], int n, SDL_Surface *screen){
    int i;
    SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
    for (i=0;i<n;i++){
        if (T[i].state == 0)
            SDL_BlitSurface(T[i].def_button, &(T[i].src), screen, &(T[i].dest));
        if (T[i].state == 1)
            SDL_BlitSurface(T[i].hov_button, &(T[i].src), screen, &(T[i].dest));
        if (T[i].state == 2)
            SDL_BlitSurface(T[i].act_button, &(T[i].src), screen, &(T[i].dest));
    }
}

char get_menu_event(bouton T[], int n){
    int x, y, i;
    char r='z';
    SDL_GetMouseState(&x,&y);
    for (i=0; i<n; i++){
        if( x >= T[i].dest.x && x <= (T[i].dest.x+T[i].dest.w) && y >= T[i].dest.y && y <= (T[i].dest.y+T[i].dest.h))
            r=T[i].action;
    }
    return r;
}

void options_menu(SDL_Surface *screen){
    int bw=270, bh=180, done=0;
    bouton T[2];
    //Bouton new game
    T[0].def_button = IMG_Load("images/NG_def.png");
    T[0].hov_button = IMG_Load("images/NG_hov.png");
    T[0].act_button = IMG_Load("images/NG_act.png");
    T[0].src.x=0;
    T[0].src.y=0;
    T[0].src.w=bw;
    T[0].src.h=bh;
    T[0].dest.x=(screen->w-bw)/2;
    T[0].dest.y=(screen->h-2*bh)/3;
    T[0].dest.w=bw;
    T[0].dest.h=bh;
    //Bouton load game
    T[1].def_button = IMG_Load("images/LG_def.png");
    T[1].hov_button = IMG_Load("images/LG_hov.png");
    T[1].act_button = IMG_Load("images/LG_act.png");
    T[1].src.x=0;
    T[1].src.y=0;
    T[1].src.w=bw;
    T[1].src.h=bh;
    T[1].dest.x=(screen->w-bw)/2;
    T[1].dest.y=bh+(screen->h-2*bh)/3;
    T[1].dest.w=bw;
    T[1].dest.h=bh;
    update_button_state(T,2);
    SDL_Flip(screen);
    while(!done){
        SDL_Event event;
        while (SDL_PollEvent(&event)){
                switch (event.type){
                    case SDL_MOUSEMOTION:
                        update_button_state(T, 2);
                    break;
                    case SDL_MOUSEBUTTONDOWN:
                        update_button_state(T, 2);
                    break;
                    case SDL_MOUSEBUTTONUP:
                        update_button_state(T, 2);
                    break;
                    case SDL_QUIT:
                        done=1;
                    break;
                    case SDL_KEYDOWN:{
                        switch (event.key.keysym.sym){
                            case SDLK_ESCAPE:
                                printf("escape");
                                done=1;
                            break;
                            default:
                            break;
                        }
                    }
                }
            display_menu(T,2,screen);
            SDL_Flip(screen);
        }
    }
    int i;
    for(i=0;i<2;i++){
        SDL_FreeSurface(T[i].def_button);
        SDL_FreeSurface(T[i].hov_button);
        SDL_FreeSurface(T[i].act_button);
    }
}

char PNG_MainMenu(SDL_Surface *screen){
    //bw=button width bh=button height
    int bw=1.5*screen->h/4,bh=screen->h/4;
    char mode='m';
    //Rectangle qui va contenir les quatres boutons
    SDL_Rect ButtonRect;
    ButtonRect.x=((screen->w)-1.5*screen->h/2)/2;
    ButtonRect.y=screen->h/2;
    ButtonRect.w=1.5*screen->h/2;
    ButtonRect.h=screen->h/2;
    SDL_Surface *title =IMG_Load("images/MainMenu_Title_48.png");
    SDL_Rect ttl_src;
    ttl_src.x=0;
    ttl_src.y=0;
    ttl_src.w=title->w;
    ttl_src.h=title->h;
    SDL_Rect ttl_dest;
    ttl_dest.x=(screen->w-title->w)/2;
    ttl_dest.y=((screen->h/2)-title->h)/2;
    ttl_dest.w=title->w;
    ttl_dest.h=title->h;
    //Tableau contenant tout les boutons du menu principal
    bouton T[4];
    //Bouton new game
    T[0].def_button = IMG_Load("images/NG_def.png");
    T[0].hov_button = IMG_Load("images/NG_hov.png");
    T[0].act_button = IMG_Load("images/NG_act.png");
    T[0].src.x=0;
    T[0].src.y=0;
    T[0].src.w=bw;
    T[0].src.h=bh;
    T[0].dest.x=ButtonRect.x;
    T[0].dest.y=ButtonRect.y;
    T[0].dest.w=bw;
    T[0].dest.h=bh;
    T[0].action='n';
    //Bouton load game
    T[1].def_button = IMG_Load("images/LG_def.png");
    T[1].hov_button = IMG_Load("images/LG_hov.png");
    T[1].act_button = IMG_Load("images/LG_act.png");
    T[1].src.x=0;
    T[1].src.y=0;
    T[1].src.w=bw;
    T[1].src.h=bh;
    T[1].dest.x=ButtonRect.x+bw;
    T[1].dest.y=ButtonRect.y;
    T[1].dest.w=bw;
    T[1].dest.h=bh;
    T[1].action='l';
    //Bouton option
    T[2].def_button = IMG_Load("images/OP_def.png");
    T[2].hov_button = IMG_Load("images/OP_hov.png");
    T[2].act_button = IMG_Load("images/OP_act.png");
    T[2].src.x=0;
    T[2].src.y=0;
    T[2].src.w=bw;
    T[2].src.h=bh;
    T[2].dest.x=ButtonRect.x;
    T[2].dest.y=ButtonRect.y+bh;
    T[2].dest.w=bw;
    T[2].dest.h=bh;
    T[2].action='o';
    //Bouton quitter
    T[3].def_button = IMG_Load("images/QT_def.png");
    T[3].hov_button = IMG_Load("images/QT_hov.png");
    T[3].act_button = IMG_Load("images/QT_act.png");
    T[3].src.x=0;
    T[3].src.y=0;
    T[3].src.w=bw;
    T[3].src.h=bh;
    T[3].dest.x=ButtonRect.x+bw;
    T[3].dest.y=ButtonRect.y+bh;
    T[3].dest.w=bw;
    T[3].dest.h=bh;
    T[3].action='q';
    update_button_state(T, 4);
    while(mode=='m'){
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                case SDL_MOUSEMOTION:
                    update_button_state(T, 4);
                break;
                case SDL_MOUSEBUTTONDOWN:
                    update_button_state(T, 4);
                break;
                case SDL_MOUSEBUTTONUP:
                    update_button_state(T, 4);
                    switch (get_menu_event(T,4)){
                        case'q':
                            mode='q';
                            break;
                        case 'o':
                            options_menu(screen);
                            break;
                        case 'l':
                            //mode=load_menu(screen);
                            break;
                        case 'n':
                            mode = 'n';
                            break;
                    }
                break;
                case SDL_QUIT:
                    mode = 'q';
                break;
                case SDL_KEYDOWN:{
                    switch (event.key.keysym.sym){
                        case SDLK_ESCAPE:
                            printf("escape");
                            mode = 'q';
                        break;
                        default:
                        break;
                    }
                }
            }
        }
        //if (SDL_MouseButtonEvent.button == SDL_BUTTON_LEFT){}
            //r=get_main_menu_event(T);
        //update_button_state(T, 4);
        display_menu(T, 4, screen);
        SDL_BlitSurface(title, &ttl_src, screen, &ttl_dest);
        SDL_Flip(screen);
    }
/*    SDL_Surface *Title = IMG_Load ("images/MainMenu_Title_48.png");
    SDL_Rect src;
    src.x=0;
    src.y=0;
    src.w=Title->w;
    src.h=Title->h;
    SDL_Rect dest;
    dest.x=Title->w;
    dest.y=Title->h;
    dest.w=0;
    dest.h=0;
    Anim_Alpha (Title, 48, 10000, &src, &dest, screen);*/
    int i;
    for(i=0;i<4;i++){
        SDL_FreeSurface(T[i].def_button);
        SDL_FreeSurface(T[i].hov_button);
        SDL_FreeSurface(T[i].act_button);
    }
    return mode;
}
